<template>
	<view class="container">
		<view class="order-list">
			<view class="order-item" v-for="(item, index) in orderList" :key="index" @click="goOrderDetail(item.orderId)">
				<view class="seller">
					<view class="avatar">
						<u-avatar size=50 :src="item.sellerAvatar"></u-avatar>
						<view class="seller-name">{{item.sellerNickName}}</view>
					</view>
					<view class="remark">{{item.remark}}</view>
				</view>
				<view class="content">
					<image mode="aspectFill" :src="item.pic"></image>
					<view class="desc">
						<view class="name">{{item.goodsName}}</view>
						<view class="price">实付款：<text>￥{{item.price}}</text></view>
						<view class="handle">
							<u-button size="mini" shape="circle" @click="call(item.qqnumber)">联系卖家</u-button>
							<!-- 交易中显示按钮 -->
							<u-button v-if="item.status == 1" size="mini" type="error" shape="circle" @click="cancel(item.orderId)">取消订单</u-button>
							<!-- 交易中或者卖家确认显示按钮 -->
							<u-button v-if="item.status == 1 || item.status == 4" size="mini" type="primary" shape="circle" @click="confirm(item.orderId)">交易完成</u-button>
						</view>
					</view>
					
				</view>
			</view>
		</view>
		
		<u-empty v-if="orderList.length == 0" mode="list"></u-empty>
		
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import {
		getBuyerOrderList,orderCancel,orderConfirm
	} from "@/api/order.js";
	import {
		getDicts
	} from "@/api/dict-data.js";
	export default {
		data() {
			return {
				pageNum: 1,
				pageSize: 10,
				orderList: [],    // 订单列表
				orderStatusList: [], // 订单状态字典列表
				flag: false      // 触底标识
			}
		},
		methods: {
			// 获取订单列表
			async getOrderList() {
				let params = {}
				params.pageNum = this.pageNum;
				params.pageSize = this.pageSize;
				
				// 查询我买到的订单列表
				await getBuyerOrderList(params).then(res => {
					// 数据追加
					this.orderList = [...this.orderList, ...res.rows]
					// console.log(this.orderList)
				})
				
				// 查询字典：商品收取类型
				await getDicts('hy_deal_order_status').then(res => {
					this.orderStatusList = res.data
				})
				
				// 订单状态遍历显示
				for( let order of this.orderList){
				    for( let status of this.orderStatusList){
				        if (status.dictValue == order.status) {
							order.remark = status.dictLabel
						}
				    }
				}
			},
			// 跳转到订单详情页
			goOrderDetail(orderId) {
				this.$u.route('/pages/order-detail/order-detail', {
						orderId: orderId
					}
				)
			},
			// 联系（买家，卖家）
			call(qqnumber) {
				plus.runtime.openURL(`mqqwpa://im/chat?chat_type=wpa&uin=${qqnumber}`, function (res) {
					plus.nativeUI.alert("本机没有安装QQ，无法启动");  
				});
			},
			// 取消订单
			cancel(orderId) {
				uni.showModal({
					title: '温馨提示',
					content: "确定取消该订单吗？",
					success: (res) => {
						if (res.confirm) {
							orderCancel(orderId).then(res => {
								this.pageNum = 1
								this.orderList = []
								this.getOrderList()
								this.$refs.uToast.show({
									title: '订单已取消',
									type: 'success'
								})
							})
						}
					}
				})
			},
			confirm(orderId){
				uni.showModal({
					title: '温馨提示',
					content: "确认交易完成了吗？",
					success: (res) => {
						if (res.confirm) {
							orderConfirm(orderId).then(res => {
								this.pageNum = 1
								this.orderList = []
								this.getOrderList()
								this.$refs.uToast.show({
									title: '订单已确认',
									type: 'success'
								})
							})
						}
					}
				})
			}
			
		},
		// 下拉刷新
		onPullDownRefresh() {
			this.pageNum = 1
			this.orderList = []
			this.getOrderList()
			uni.stopPullDownRefresh();
		},
		onLoad() {
			this.getOrderList()
		}
	}
</script>

<style lang="scss">
	@import "./my-buy.scss";
</style>
